Skip to contentMethod: execute(QuerySpecification, OntologySnapshot)
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.owlapi.query;
19:
20: import cz.cvut.kbss.ontodriver.ResultSet;
21: import cz.cvut.kbss.ontodriver.owlapi.connector.Connector;
22: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
23: import cz.cvut.kbss.ontodriver.owlapi.exception.OwlapiDriverException;
24: import cz.cvut.kbss.ontodriver.owlapi.exception.ReasonerNotAvailableException;
25: import cz.cvut.kbss.owl2query.engine.OWL2QueryEngine;
26: import cz.cvut.kbss.owl2query.model.QueryResult;
27: import cz.cvut.kbss.owl2query.model.owlapi.OWLAPIv3OWL2Ontology;
28: import org.semanticweb.owlapi.model.OWLObject;
29: import org.semanticweb.owlapi.reasoner.OWLReasoner;
30:
31: public class LiveOntologyStatementExecutor implements StatementExecutor {
32:
33: private final Connector connector;
34:
35: public LiveOntologyStatementExecutor(Connector connector) {
36: this.connector = connector;
37: }
38:
39: @Override
40: public ResultSet executeQuery(QuerySpecification query) throws OwlapiDriverException {
41: final ResultSet resultSet = connector.executeRead(snapshot -> {
42: final QueryResult<OWLObject> res = execute(query, snapshot);
43:
44: return res != null ? AbstractResultSet.createResultSet(res, query.getStatement(), query.getQuery()) : null;
45: });
46: if (resultSet == null) {
47: throw new OwlapiDriverException("Unable to execute query " + query);
48: }
49: return resultSet;
50: }
51:
52: private QueryResult<OWLObject> execute(QuerySpecification query, OntologySnapshot snapshot) {
53:• if (snapshot.getReasoner() == null) {
54: throw new ReasonerNotAvailableException("Cannot execute query without a reasoner.");
55: }
56: final OWLReasoner reasonerToUse =
57:• query.isDisableInference() ? getNoInferenceReasoner(snapshot) : snapshot.getReasoner();
58: final OWLAPIv3OWL2Ontology ont = new OWLAPIv3OWL2Ontology(snapshot.getOntologyManager(),
59: snapshot.getOntology(), reasonerToUse);
60:
61: return OWL2QueryEngine.exec(query.getQuery(), ont);
62: }
63:
64: private static OWLReasoner getNoInferenceReasoner(OntologySnapshot snapshot) {
65: return new NoOpReasoner(snapshot.getOntology());
66: }
67:
68: @Override
69: public void executeUpdate(QuerySpecification query) {
70: connector.executeWrite(snapshot -> execute(query, snapshot));
71: }
72: }